package com.duowan.cms.util;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import magick.CompositeOperator;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
/*
** 
*/  
public class JmagickHelper {  

	public static enum MaskPosition {
		TOP_LEFT{//顶部左
			@Override
			public int getValue(){
				return 1;
			}
		},TOP_CENTER{//顶部中
			@Override
			public int getValue(){
				return 2;
			}
		},	TOP_RIGHT{//顶部右
			@Override
			public int getValue(){
				return 3;
			}
		},	CENTER_LEFT{//中部左
			@Override
			public int getValue(){
				return 4;
			}
		},	CENTER{//正中央
			@Override
			public int getValue(){
				return 5;
			}
		},	CENTER_RIGHT{//中部右
			@Override
			public int getValue(){
				return 6;
			}
		},	BOTTOM_LEFT{//底部左
			@Override
			public int getValue(){
				return 7;
			}
		},BOTTOM_CENTER{//底部中
			@Override
			public int getValue(){
				return 8;
			}
		},BOTTOM_RIGHT{//底部右
			@Override
			public int getValue(){
				return 9;
			}
		};
		
		public abstract int getValue();
		public static MaskPosition getInstance(int position){
			switch(position) 
			{
			  case 1: return TOP_LEFT;
			  case 2: return TOP_CENTER;
			  case 3: return TOP_RIGHT;
			  case 4: return CENTER_LEFT ;
			  case 5: return CENTER;
			  case 6: return CENTER_RIGHT;
			  case 7: return BOTTOM_LEFT;
			  case 8: return BOTTOM_CENTER;
			  case 9: return BOTTOM_RIGHT;
			  default: return null;
			}
		}
	}
	

 
   /** 
    * 缩放图片--重置图片的大小 
    * @param srcPackage 源图片文件夹 
    * @param srcFileName   源图片名称 
    * @param toPackage 目的图片文件夹 
    * @param toFileName    目的图片名称 
    * @param w 目的图片宽度 
    * @param h 目的图片高度 
    * @throws IOException 
    */  
   public void resize(String srcPackage, String srcFileName, String toPackage,  
           String toFileName, int w, int h) {  
       try {  
           // Resize  
           System.setProperty("jmagick.systemclassloader", "no");  
           ImageInfo info = new ImageInfo(srcPackage + srcFileName);     
           MagickImage image = new MagickImage(info);  
           MagickImage scaled = image.scaleImage(w, h);    // 小图片文件的大小.  
           scaled.setFileName(toPackage + toFileName);       
           scaled.writeImage(info);  
       } catch (Exception ex) {  
           System.out.println(ex);  
       }  
   }  
 
   /** 
    * 裁剪图片 
    *  
    * @param srcPackage  源图片文件夹 
    * @param srcFileName   源图片名称 
    * @param toPackage  目的文件件 
    * @param toFileName   目的文件名 
    * @param x    图片左上角的x坐标 
    * @param y     图片左上角的y坐标 
    * @param w   截取图片的宽度 
    * @param h   截取图片的高度 
    */  
   public void cropImage(String srcPackage, String srcFileName,  
           String toPackage, String toFileName, int x, int y, int w, int h) {  
       MagickImage cropImage = null;  
       try {  
           ImageInfo infoS = new ImageInfo(srcPackage + srcFileName);  
           MagickImage image = new MagickImage(infoS);  
           Rectangle rect = new Rectangle(x, y, w, h);  
           cropImage = image.cropImage(rect);  
           String toPath = toPackage + toFileName;  
           cropImage.setFileName(toPath);  
           cropImage.writeImage(infoS);  
       } catch (Exception e) {  
           e.printStackTrace();  
       } finally {  
           if (cropImage != null) {  
               cropImage.destroyImages();  
           }  
       }  
   }  
 
   /**
    * 合成(打水印)
    * @param srcPath
    * @param logPath
    * @param distPath
    * @throws MagickException
    */
   public  void  composit(String srcPath ,  String logPath, String distPath , JmagickHelper.MaskPosition maskPosition) throws MagickException{
	   ImageInfo info = new ImageInfo();
	   MagickImage srcPic = new MagickImage( new ImageInfo( srcPath) );
	   MagickImage logPic = new MagickImage( new ImageInfo( logPath ) );
	   Dimension srcPicSize= srcPic.getDimension();
	   Dimension logPicSize= logPic.getDimension();
	   
	   int []position = this.getMaskPosition(srcPicSize.width, srcPicSize.height, logPicSize.width, logPicSize.height, maskPosition);
		srcPic.compositeImage(CompositeOperator.AtopCompositeOp, logPic,position[0],  position[1]);
	   srcPic.setFileName(distPath);
	   srcPic.writeImage(info);
	   if(srcPic!=null)srcPic.destroyImages();
	   if(logPic!=null)logPic.destroyImages();
   }
     
   /** 
    * 合并图片 
    * @param srcPathList 源图片路径名称列表 
    * @param toPathFile    合并后图片路径名称 
    * @param orientation 合并方位：横向(+)、纵向(-) 
    * @return 
    */  
   public  void convert(List srcPathList,String toPathFile,String orientation){  
       /*  合并图片命令格式: 
        * convert +append 1.jpg 2.jpg 3.jpg ... 0.jpg //横向合并（最后一个为合并成功的文件名称） 
        * convert -append 1.jpg 2.jpg 3.jpg ... 0.jpg  //纵向合并（最后一个为合并成功的文件名称） 
       */  
       StringBuffer command = new StringBuffer("");  
       command.append("convert ");  
       command.append(orientation+"append ");  
       int length = srcPathList.size();  
       for(int i=0;i<length;i++){  
           command.append(srcPathList.get(i)+" ");  
       }  
       command.append(toPathFile);  
       String[] str = {command.toString()};  
       this.exec(str);  
   }  
     
   /** 
    * 调用windows的命令行并执行命令 
    * @param command 命令行窗口中要执行的命令字符串 
    */  
   private   void  exec(String[] command) {    
           Process proc;    
           InputStream is = null;  
           String comman = command[0];  
           System.out.println("命令为："+comman);  
           String[] cmd = {"cmd.exe","/c",comman};  
           try {  
               proc = Runtime.getRuntime().exec(cmd);  
               int result = proc.waitFor();  
               System.out.println("Process result:" + result);  
               is = proc.getInputStream();  
               byte[] b = new byte[1024];  
               is.read(b);  
               System.out.println("b:" + b);  
               is.close();  
           } catch (Exception e) {  
               e.printStackTrace();  
           }  
   }    
 
	/**
	 * 返回水印图片在原图的位置(宽*高)width*height
	 * @param srcW   源图宽
	 * @param srcH   源图高
	 * @param maskW  水印宽
	 * @param maskH  水印高  
	 * @param pos    位置
	 * @return
	 */
	private  int[] getMaskPosition(int srcW,int srcH,int maskW,int maskH,JmagickHelper.MaskPosition maskPosition)
	{
		int[] coord = new int[]{-1,-1};
		
		if(srcW<=maskW || srcH<=maskH)
		   return coord;
		
		int x=0,y=0;
		
		//水印位置定位9点
		switch(maskPosition.getValue() ) 
		{
		  case 1:x=0;y=0;break;
		  case 2:y=0;x=(srcW-maskW)/2;break;
		  case 3:y=0;x=srcW-maskW;break;
		  case 4:x=0;y=(srcH-maskH)/2;break;
		  case 5:y=(srcH-maskH)/2;x=(srcW-maskW)/2;break;
		  case 6:x=srcW-maskW;y=(srcH-maskH)/2;break;
		  case 7:y=srcH-maskH;x=0;break;
		  case 8:x=(srcW-maskW)/2;y=srcH-maskH;break;
		  case 9:y=srcH-maskH;x=srcW-maskW;break;
		  default:y=srcH-maskH;x=srcW-maskW;
		}
		coord[0]=x;
		coord[1]=y;
		return coord;
	}

}  
